var app = angular.module('myApp',['ui.router']);
var resultData = [];

//左侧菜单部分controller
app.controller('content',['$scope','$http',function($scope,$http){
    $http({
        method:'get',
        url:'data/leftMenu.json'
    }).then(function(res){
        //console.log(res);
        $scope.list = res.data;
    });
}]);

app.component('myBookList',{
    templateUrl:'list.html',
    controller:function($scope){
        $scope.$on('changeList',function(e,data){
            console.log(data);
            $scope.items = data;
        })
    }
});

app.service('getList',['$http',function($http){
    var createList = {};
    var data = [];
    return {
        createList: function (id) {
            return $http({
                method: 'get',
                url: 'data/books.json'
            }).then(function(res){
                data = [];
                for(var i=0;i<res.data.length;i++){
                    if(id == 1){
                        data = res.data;
                    }else if(id == res.data[i].tag){
                        data.push(res.data[i]);
                    }
                }
                return data;
            });
        }
    }
}]);
app.config(function($stateProvider){
    var bookListState = {
        name:'bList',
        url:'/list:{listId}',
        component:'myBookList',
        resolve:{
            bList:function(getList,$transition$,$rootScope){
                getList.createList($transition$.params().listId).then(function(data){
                    console.log(data);
                    $rootScope.$broadcast('changeList',data);
                })
            }
        }
    };
    $stateProvider.state(bookListState);
});





